// fillings for Team header
#include "Includes.hpp" // includes for project
#include "Team.hpp" // class declarations

Team::Team() { // default constructor // should not be used
    cout << "\n\tyou are doing it wrong!\n\tsee team class"; // print error
}

Team::Team(ifstream* in) {
    int tempid = 0; // temporary variable to store player id in
    name = robio.readFileString(in); //(*in).ignore(2, '#'); // read name ignore delimiter
    homeaddress = robio.readFileString(in); // read 'homeaddress'
//    (*in).ignore(2, '#'); // ignore delimiter
    (*in) >> playercount; (*in).ignore(2, '#'); // read playercount ignore delimiter
    for (int tvar = 0; tvar <= playercount; tvar++) { // go through all players
	(*in) >> tempid; // read temporary id
	(*in).ignore(2, '#'); // ignore delimiter
	if (!(players.playersLlist->in_list(tempid))) { // if player is not in 'playersLlist'
	    Player* tempplayer = new Player(in, tempid); // make new player from file-stream
	    players.playersLlist->add(tempplayer); // add player to list
	} else {
	    in->ignore(STRINGLENGTH+2, '#'); // ignore this players name
	    in->ignore(STRINGLENGTH+2, '#'); // ignore this players address
	} // if !in_list() end
	if (playercount < MAXPLAYERS) { // if team array is !full
	    playersid[++playercount] = tempid; // add new player id to team array
	} else {
	    cout << "\n\tteam " << name << " is full, see Team::Team(ifstream*)"; //print error
	} //if team is !full end
    } // for tvar end
}

void Team::writeToFile(ofstream* out) {
    Player* tempplayer; // temporary player to work on
    (*out) << name << " #" // write team name and delimiter
	   << homeaddress << " #" // write homeaddress and delimiter
	   << playercount << " #"; // write playercount and delimiter
    for (int tvar = 0; tvar <= playercount; tvar++) { // go through all players
	tempplayer = (Player*)players.playersLlist->remove(playersid[tvar]); // get player number 'tvar'
	tempplayer->writeToFile(out); // write players info
	players.playersLlist->add(tempplayer); // add player back into list
    } // for tvar end
}

void Team::display() {
    cout << "\n\tTeam:\t" << name;
    cout << "\n\tAddress:\t" << homeaddress;
    cout << "\n\tNumber of players:\t" << playercount;
    for(int i = 0; i <= playercount; i++) { // go through all players in team
	players.playersLlist->display_element(playersid[i]); // display one
    }
}
